package com.hddz.acquisition.activity.stock;

import java.util.HashMap;
import java.util.Map;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.hddz.acquisition.GlobalConst;
import com.hddz.acquisition.R;
import com.hddz.acquisition.activity.BaseActivity;
import com.hddz.acquisition.fragment.stock.FragmentFree1SumList;
import com.hddz.acquisition.fragment.stock.FragmentWarehouseOrInvSum;
import com.hddz.acquisition.model.JsonRespose;
import com.hddz.acquisition.model.StockJsonRespose;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

@ContentView(R.layout.activity_stock_scan)
public class InvDetailActivity extends BaseActivity {
    private final String TAG = "GroundingActivity";
    @ViewInject(R.id.tab_vpager)
    private ViewPager tab_vpager;
    @ViewInject(R.id.lbl_main_title)
    private TextView lbl_main_title;
    @SuppressLint("UseSparseArrays")
    private Map<Integer, Fragment> mCacheFragments = new HashMap<Integer, Fragment>();
    private Fragment mFm = null;
    FragmentWarehouseOrInvSum fragmentWarehouseOrInvSum = null;
    FragmentFree1SumList fragmentFree1SumList = null;
    private Bundle bundle = null;

    String currentCmd = "9003";
    String targetwarehouse, invcode, free1;

    @SuppressLint("HandlerLeak")
    private Handler m1Handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case GlobalConst.Handler_ADD_RESPONSE:
                    StockJsonRespose json = (StockJsonRespose) msg.obj;
                    fragmentWarehouseOrInvSum.updateResponse(json);
                    fragmentFree1SumList.updateList(json.response.ResponseList);
                    break;
                default:
                    break;
            }
        }

        ;
    };

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        lbl_main_title.setText(R.string.menu_spxx);
        tab_vpager.setAdapter(new MainViewPagerAdapter(getSupportFragmentManager()));
        tab_vpager.setOffscreenPageLimit(2);


        targetwarehouse = getIntent().getStringExtra("TARGETWAREHOUSE");
        invcode = getIntent().getStringExtra("INVCODE");
        free1 = getIntent().getStringExtra("FREE1");

        baseSendCommandWithFree(currentCmd, invcode, free1);
    }

    @Override
    public void addResponse(JsonRespose json) {

    }

    @Override
    public void addErr(String err) {
        Toast.makeText(InvDetailActivity.this, err, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void receiveCode(String code) {

    }

    @Override
    public void backDo() {
        onBack(null);
    }

    @Override
    public void finishDo() {
        finish();
    }

    private class MainViewPagerAdapter extends FragmentPagerAdapter {

        public MainViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public Fragment getItem(int position) {
            if (mCacheFragments.containsKey(position)) {
                mFm = mCacheFragments.get(position);
            } else {
                switch (position) {
                    case 1:
                        fragmentWarehouseOrInvSum = FragmentWarehouseOrInvSum.newInstance();
                        bundle = new Bundle();
                        bundle.putString("COMMAND", currentCmd);
                        fragmentWarehouseOrInvSum.setArguments(bundle);
                        mFm = fragmentWarehouseOrInvSum;
                        break;
                    case 0:
                        fragmentFree1SumList = FragmentFree1SumList.newInstance();
                        bundle = new Bundle();
                        bundle.putString("COMMAND", currentCmd);
                        fragmentFree1SumList.setArguments(bundle);
                        mFm = fragmentFree1SumList;
                        break;
                    default:
                        break;
                }
                mCacheFragments.put(position, mFm);
            }
            return mFm;
        }
    }

    @Event(value = R.id.btn_back)
    private void onBack(View view) {
        finish();
    }
}
